using UnityEngine;

public class PlayerInAirState : PlayerState
{
    private bool _coyoteTime;
    private bool _Dashinput;
    private bool _isGrabed;

    //Check
    private bool _isGrounded;

    private bool _isTouchingLedge;

    private bool _isTouchingWall;
    private bool _isTouchingWallBack;
    private bool _jumpInput;
    private bool _PrimaryAttackInput;
    private bool _SecondaryAttackInput;

    //��������һ���µ�coyote time��������ش�walltouching״̬������
    //Input
    private int _Xinput;

    public PlayerInAirState(Player player, PlayerSM stateMachine, PlayerData playerData, string animBoolName, Core core)
        : base(player, stateMachine, playerData, animBoolName, core)
    {
    }

    public override void DoChecks()
    {
        base.DoChecks();

        _isGrounded = _core.Collision.Grounded;
        _isTouchingWall = _core.Collision.WallFront;
        _isTouchingWallBack = _core.Collision.WallBack;
        _isTouchingLedge = _core.Collision.LedgeHorizontal;

        if (_isTouchingWall && !_isTouchingLedge)
            _player.LedgeClimbState.SetDetectedPosition(_player.transform.position);
    }

    public override void Enter()
    {
        base.Enter();
    }

    public override void Exit()
    {
        base.Exit();
    }

    public override void LogicUpdate()
    {
        base.LogicUpdate();
        CheckCoyoteTime();

        _Xinput = LightInputManager.Instance.NormalInputX;
        _jumpInput = LightInputManager.Instance.JumpInput;
        _isGrabed = LightInputManager.Instance.GrabInput;
        _Dashinput = LightInputManager.Instance.DashInput;

        _PrimaryAttackInput = LightInputManager.Instance.AttackInputs[(int)CombatInputs.Primary];
        //_SecondaryAttackInput = LightInputManager.Instance.AttackInputs[(int)CombatInputs.Secondary];

        if (_PrimaryAttackInput)
        {
            _stateMachine.ChangeState(_player.PrimaryAttackState);
        }
        else if (_SecondaryAttackInput)
        {
            _stateMachine.ChangeState(_player.SecondAttackState);
        }
        else if (_isGrounded && _core.Movement.CurrentVelocity.y < 0.01f)
        {
            _stateMachine.ChangeState(_player.LandState);
        }
        else if (_isTouchingWall && !_isTouchingLedge && !_isGrounded)
        {
            _stateMachine.ChangeState(_player.LedgeClimbState);
        }
        else if (_jumpInput && (_isTouchingWall || _isTouchingWallBack))
        {
            _player.WallJumpState.DetermineWallJumpDirection(_isTouchingWall);
            _stateMachine.ChangeState(_player.WallJumpState);
        }
        else if (_jumpInput && _player.JumpState.CanJump())
        {
            _stateMachine.ChangeState(_player.JumpState);
        }
        else if (_isTouchingWall && _isGrabed && _isTouchingLedge) //ADD:&& _isTouchingLedge Ϊ�˽��һ������bug
        {
            _stateMachine.ChangeState(_player.WallGrabState);
        }
        else if (_isTouchingWall && _Xinput == _core.Movement.FacingDirection && _core.Movement.CurrentVelocity.y < 0f)
        {
            _stateMachine.ChangeState(_player.WallSlideState);
        }
        else if (_Dashinput && _player.DashState.CheckIfCanDash())
        {
            _stateMachine.ChangeState(_player.DashState);
        }
        else
        {
            _core.Movement.CheckIfShouldFilp(_Xinput);
            _core.Movement.SetVelocityX(_playerData._moveSpeed * _Xinput);

            _player.Anim.SetFloat("yVelocity", _core.Movement.CurrentVelocity.y);
            _player.Anim.SetFloat("xVelocity", Mathf.Abs(_core.Movement.CurrentVelocity.x));
        }
    }

    public override void PhysicsUpdate()
    {
        base.PhysicsUpdate();
    }

    private void CheckCoyoteTime()
    {
        if (_coyoteTime && Time.time > _startTime + _playerData._coyoteTime)
        {
            _coyoteTime = false;
            _player.JumpState.DecreaseCountofJump();
        }
    }

    public void StartCoyoteTime()
    {
        _coyoteTime = true;
    }
}